package mxmf.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TimeUtils {

    private static final Logger logger = LoggerFactory.getLogger(TimeUtils.class);
    

    private TimeUtils() {
        throw new IllegalStateException("Utility class");
    }

    public static boolean equals(Date sourceTime, Date destTime) {
        if (sourceTime == null || destTime == null) {
            return false;
        }
        return sourceTime.getTime() == destTime.getTime();
    }


    public static Date getStartTime(String startTime) {
        Date beginTime = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        if (StringUtils.isNotBlank(startTime)) {
            try {
                beginTime = dateFormat.parse(startTime);
            } catch (ParseException e) {
                logger.error(e.toString(), e);
            }
        }

        if (beginTime == null) {
            startTime = dateFormat.format(new Date());
            try {
                beginTime = dateFormat.parse(startTime);
            } catch (ParseException e) {
                logger.error(e.toString(), e);
            }
        }

        return beginTime;

    }

    public static Date getEndTime(String endTime, Date startTime) {
        Date endTimeDate = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        if (StringUtils.isNotBlank(endTime)) {
            try {
                endTimeDate = dateFormat.parse(endTime);
            } catch (ParseException e) {
                logger.error(e.toString(), e);
            }
        }

        if (endTime == null) {
            endTimeDate = startTime;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTimeDate);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * @param startTime
     * @return yyyyMMdd
     */
    public static String getTimeYMD(Date dateTime) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        return dateFormat.format(dateTime);

    }

    public static Date getDate(String date) {
        try {
            SimpleDateFormat dateFormatFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateFormatFull.parse(date);
        } catch (ParseException e) {
            logger.error(e.toString(), e);
            return null;
        }
    }

    public static String getDate(Date date) {
        SimpleDateFormat dateFormatFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormatFull.format(date);
    }

    public static Date getStartTime(Date time) {
        return getStartTime(time, 0);
    }

    public static Date getEndTime(Date time) {
        return getEndTime(time, 0);
    }

    public static String getYyyyMMdd(int i) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, i);
        return dateFormat.format(calendar.getTime());
    }

    public static Date getStartTime(Date time, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        if (i != 0) {
            calendar.add(Calendar.DATE, i);
        }
        return calendar.getTime();
    }

    public static Date getEndTime(Date time, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        if (i != 0) {
            calendar.add(Calendar.DATE, i);
        }
        return calendar.getTime();
    }

}
